package com.qdlc.p2p.dal.model;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.ValidateUtil;
import com.qdlc.p2p.dal.dto.EnjoyGoods;

/**
 * Created by D on 2016/1/20.
 */
public class EnjoyGoodsModel extends EnjoyGoods {

    private Logger logger = Logger.getLogger(EnjoyGoodsModel.class);

    /** 状态-有库存 */
    public static final byte ENJOY_GOODS_ENABLE = 1;
    /** 状态-无库存 */
    public static final byte ENJOY_GOODS_DISABLE = 2;

    /** 当前页码 */
    private int page = 1;

    /** 每页数据条数 */
    private int rows = 5;

    /**
     * 模块
     */
    private String module ;

    private String searchName;

    public static EnjoyGoodsModel instance(EnjoyGoods enjoyGoods) {
        EnjoyGoodsModel enjoyGoodsModel = new EnjoyGoodsModel();
        BeanUtils.copyProperties(enjoyGoods, enjoyGoodsModel);
        return enjoyGoodsModel;
    }

    public EnjoyGoods prototype() {
        EnjoyGoods enjoyGoods = new EnjoyGoods();
        BeanUtils.copyProperties(this, enjoyGoods);
        return enjoyGoods;
    }

    /*************************define for manage  start**********************************/
    /**
     * 检查提交的数据格式
     *
     * @return
     */
    public int validModelData() {
        if (!ValidateUtil.isAmount(getCostPrice())) {
            throw new BusinessException(MessageUtil.getMessage("MF102004004"), 1);
        }
        if (!ValidateUtil.isAmount(getPrice())) {
            throw new BusinessException(MessageUtil.getMessage("MF102004004"), 1);
        }
        if(!ValidateUtil.isInteger(getPosition())){
            throw new BusinessException(MessageUtil.getMessage("MF102100004"), 1);
        }
        if(getPosition()==0){
            throw new BusinessException(MessageUtil.getMessage("MF102100005"), 1);
        }
        if(getPrefecture()==0){
            throw new BusinessException(MessageUtil.getMessage("MF102100006"), 1);
        }
        if(!ValidateUtil.isInteger(getAmount())){
            throw new BusinessException(MessageUtil.getMessage("MF102100001"), 1);
        }
        return -1;
    }

    /**
     * 添加
     * 根据EnjoyGoodsModel获得EnjoyGoods
     * @return
     * @throws Exception
     */
    public EnjoyGoods prototypeForAppend() throws Exception {
        EnjoyGoods enjoyGoods = this.prototype();
        enjoyGoods.setStatus(EnjoyGoodsModel.ENJOY_GOODS_ENABLE);
        enjoyGoods.setAddTime(DateUtil.getNow());
        enjoyGoods.setAddIp(Global.getIP());
        return enjoyGoods;
    }
    /*************************define for manage  end**********************************/

    /*************************define for core  start**********************************/
    /**
     * 为趣享标添加商品做好商品规则校验
     */
    public boolean checkGoods(EnjoyGoods enjoyGoods) throws Exception {
        if(enjoyGoods == null){
            logger.error("商品不存在，商品添加失败！");
            return false;
        }
        if(enjoyGoods.getStatus()==2){
            logger.error("商品无库存，赠送失败！id:"+enjoyGoods.getId());
            return false;
        }
        return true;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }
}
